package com.thlws.redis;

import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * @author HanleyTang
 * @date 2018-12-15
 */
public class JedisDemo {

    public static void main(String[] args) {

        try(Jedis jedis = new Jedis("localhost",6379);){
//        jedis.auth("");
            System.out.println(jedis.getDB());

            jedis.select(8);
            System.out.println(jedis.getDB());

            jedis.flushDB();
            jedis.flushAll();

            jedis.set("name", "hanley");
            jedis.set("age", "31");
            jedis.set("address", "SH");

            Set<String> keys =  jedis.keys("*");
            for (String k : keys) {
                System.out.println(k+": "+jedis.get(k));
            }

            System.out.println("db size:"+jedis.dbSize());
            jedis.move("address", 0);
            System.out.println("db size:"+jedis.dbSize());

            jedis.select(0);
            System.out.println("db size:"+jedis.dbSize());

            jedis.expire("temp", 20);//已存在的key设置失效时间
            jedis.setex("temp2", 10, "测试临时数据");//添加时设置有效时间
            System.out.println("db size:"+jedis.dbSize());
        }


    }
}
